/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!				\file		ErrDlg.cc
				\date		june 2010
				\author		TNick

				\brief		Implementation of the dialog used to display messages


    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Please read ReadMe.txt and Licence.txt in root folder
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    
*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include	"ErrDlg.h"

/*  INCLUDES    ============================================================ */
//
//
//
//
/*  DEFINITIONS    --------------------------------------------------------- */


/*  DEFINITIONS    ========================================================= */
//
//
//
//
/*  DATA    ---------------------------------------------------------------- */



/*  DATA    ================================================================ */
//
//
//
//
/*  FUNCTIONS    ----------------------------------------------------------- */


/* ------------------------------------------------------------------------- */
ErrDlg::ErrDlg			(QWidget *parent) :
		QDialog(parent)
{
	InternalInit();
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
ErrDlg::ErrDlg			(CC_Err * pOneErr, QWidget *parent):
		QDialog(parent)
{

	InternalInit();

	/* we do know the mode now */
	opm = OPMODE_SINGLE;

	/* store error pointer */
	SingleErr = pOneErr;
	MultipleErr = NULL;

	ShowAError();
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
ErrDlg::ErrDlg			(
		QList<CC_Err*> * pErrChain, QWidget *parent):QDialog(parent)
{

	InternalInit();

	/* we do know the mode now */
	opm = OPMODE_CHAIN;

	/* store chain pointer */
	MultipleErr = pErrChain;
	SingleErr = NULL;

	ShowChainError();

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void			ErrDlg::InternalInit	(void)
{
	/* can't tell operation mode yet */
	opm = OPMODE_NOINIT;
	bOptShown = false;

	/* start with user defined settings related to what we're showing */
	AllowedTypes = CC_Err::AllowedErrTypes();

	/* vertical layout groups control elements */
	QVBoxLayout *vlay = new QVBoxLayout;

	/* create OK button */
	QPushButton*	OkBtn = new QPushButton(tr("OK"));
	OkBtn->setDefault(true);
	connect(OkBtn, SIGNAL(clicked()), this, SLOT(close()));
 	vlay->addWidget(OkBtn,0,Qt::AlignTop | Qt::AlignHCenter);

	/* create text box that will display the errors */
	OutTx = new QTextEdit();
	OutTx->setReadOnly(true);
	OutTx->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
	OutTx->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
	OutTx->setLineWrapMode(QTextEdit::NoWrap);
	OutTx->setWordWrapMode(QTextOption::NoWrap);
	OutTx->setTabStopWidth(20);


	/* create show / hide button */
	ShHdBtn = new QToolButton();
	ShHdBtn->setText("Options");
	ShHdBtn->setToolButtonStyle(Qt::ToolButtonTextOnly);
	connect(ShHdBtn, SIGNAL(clicked()),
			this, SLOT(DisplayTyChange()));
 	vlay->addWidget(ShHdBtn,0,Qt::AlignTop | Qt::AlignHCenter);



	/* create check boxes for each type of error */
	for (int i = 0; i < CCERR::EIDX_COUNT; i++)
		{
			ckAry[i] = new QCheckBox(CC_Err::ErrTyTextFromIdx(i));
			if ((AllowedTypes & CC_Err::ErrIndexType(i)) != 0)
				{
					ckAry[i]->setCheckState(Qt::Checked);
				}
			ckAry[i]->setVisible(false);
			connect(ckAry[i], SIGNAL(stateChanged(int)),
					this, SLOT(ErrTypeChanged(int)));
			vlay->addWidget(ckAry[i],0,Qt::AlignTop | Qt::AlignLeft);
		}
 	vlay->addStretch(1);



	QHBoxLayout *layout = new QHBoxLayout;
	layout->addWidget(OutTx);
 	layout->addLayout(vlay);
	setWindowTitle(tr("CASP message"));


	setLayout(layout);
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
ErrDlg::~ErrDlg			()
{

}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void			ErrDlg::ShowAError		(void)
{
	OutTx->clear();

	/* the error structure has a list of typed strings */
	if (SingleErr == NULL)
		{
			return;
		}

	SingleErr->ShowEdit(AllowedTypes, OutTx);
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void			ErrDlg::ShowChainError	(void)
{
	OutTx->clear();


	/* iterate in elements from chain */
	for (int j = 0; j < MultipleErr->count(); j++)
		{
			CC_Err*		pCrtE;
			pCrtE = MultipleErr->at(j);

			pCrtE->ShowEdit(AllowedTypes, OutTx);
		} /* for (int j = 0; j < MultipleErr->count(); j++) */
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void			ErrDlg::ErrTypeChanged	(int state)
{
	Q_UNUSED(state);

	if (opm == OPMODE_NOINIT)
		return;

	AllowedTypes = (CCERR::ETYPE)0;
	for (int i = 0; i < CCERR::EIDX_COUNT; i++)
		{
			if (ckAry[i]->isChecked())
				{
					AllowedTypes = (CCERR::ETYPE)(AllowedTypes | CC_Err::ErrIndexType(i));
				}
		}

	if (AllowedTypes == 0) 
		return;

	if (opm == OPMODE_SINGLE)
		ShowAError();
	else
		ShowChainError();
}
/* ========================================================================= */

/* ------------------------------------------------------------------------- */
void			ErrDlg::DisplayTyChange		(void)
{
	if (bOptShown)
		{
			ShHdBtn->setText("Options");
			bOptShown = false;
		}
	else
		{
			ShHdBtn->setText("Hide");
			bOptShown = true;
		}
	for (int i = 0; i < CCERR::EIDX_COUNT; i++)
		{
			ckAry[i]->setVisible(bOptShown);
		}
}
/* ========================================================================= */


/*  FUNCTIONS    =========================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
